package com.clarence.dada.core.redis.start;


import com.clarence.dada.core.auth.entity.LoginUser;
import com.clarence.dada.core.auth.entity.dto.PwdRsaSecretProperties;
import com.clarence.dada.core.auth.expander.AuthConfigExpander;
import com.clarence.dada.core.auth.service.PasswordStoredEncryptApi;
import com.clarence.dada.core.auth.service.PasswordTransferEncryptApi;
import com.clarence.dada.core.auth.service.impl.BcryptPasswordStoredEncrypt;
import com.clarence.dada.core.auth.service.impl.RsaPasswordTransferEncrypt;
import com.clarence.dada.core.auth.session.SessionCookieCreator;
import com.clarence.dada.core.auth.session.SessionManagerApi;
import com.clarence.dada.core.auth.session.impl.DefaultSessionManager;
import com.clarence.dada.core.redis.RedisOperatorApi;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Set;

/**
 * 认证和鉴权模块的自动配置
 *
 * @author fengshuonan
 * @since 2020/11/30 22:16
 */
@Configuration
public class GunsAuthAutoConfiguration {

    @Resource
    private PwdRsaSecretProperties pwdRsaSecretProperties;
//
//    /**
//     * jwt操作工具类的配置
//     *
//     * @author fengshuonan
//     * @since 2020/12/1 14:40
//     */
//    @Bean
//    @ConditionalOnMissingBean(JwtApi.class)
//    public JwtApi jwtApi() {
//
//        JwtConfig jwtConfig = new JwtConfig();
//
//        // 从系统配置表中读取配置
//        jwtConfig.setJwtSecret(AuthConfigExpander.getAuthJwtSecret());
//        jwtConfig.setExpiredSeconds(AuthConfigExpander.getAuthJwtTimeoutSeconds());
//
//        return new JwtTokenOperator(jwtConfig);
//    }


    /**
     * Bcrypt方式的密码加密
     *
     * @author fengshuonan
     * @since 2020/12/21 17:45
     */
    @Bean
    @ConditionalOnMissingBean(PasswordStoredEncryptApi.class)
    public PasswordStoredEncryptApi passwordStoredEncryptApi() {
        return new BcryptPasswordStoredEncrypt();
    }

    /**
     * RSA方式密码加密传输
     *
     * @author fengshuonan
     * @since 2020/12/21 17:45
     */
    @Bean
    @ConditionalOnMissingBean(PasswordTransferEncryptApi.class)
    public PasswordTransferEncryptApi passwordTransferEncryptApi() {
        return new RsaPasswordTransferEncrypt(pwdRsaSecretProperties.getPublicKey(), pwdRsaSecretProperties.getPrivateKey());
    }

    /**
     * session cookie的创建
     *
     * @author fengshuonan
     * @since 2020/12/27 15:48
     */
    @Bean
    @ConditionalOnMissingBean(SessionCookieCreator.class)
    public SessionCookieCreator sessionCookieCreator() {
        return new SessionCookieCreator() {
            @Override
            public void expandCookieProp(Cookie cookie) {
                cookie.setHttpOnly(true);
                cookie.setPath("/");
            }
        };
    }

    /**
     * 默认的session缓存为内存缓存，方便启动
     * <p>
     * 如需替换请在项目中增加一个SessionManagerApi Bean即可
     *
     * @author fengshuonan
     * @since 2020/11/30 22:17
     */
    @Bean
    @ConditionalOnMissingBean(SessionManagerApi.class)
    public SessionManagerApi sessionManagerApi(RedisOperatorApi<LoginUser> loginUserCache, RedisOperatorApi<Set<String>> allPlaceLoginTokenCache) {
        Long sessionExpiredSeconds = AuthConfigExpander.getSessionExpiredSeconds();
        return new DefaultSessionManager(loginUserCache, allPlaceLoginTokenCache, sessionExpiredSeconds, sessionCookieCreator());
    }

    /**
     * 清空无用登录用户缓存的定时任务
     *
     * @author fengshuonan
     * @since 2021/3/30 11:32
     */
//    @Bean
//    @ConditionalOnMissingBean(ClearInvalidLoginUserCacheTimer.class)
//    public ClearInvalidLoginUserCacheTimer clearInvalidLoginUserCacheTimer(CacheOperatorApi<LoginUser> loginUserCache, CacheOperatorApi<Set<String>> allPlaceLoginTokenCache) {
//        return new ClearInvalidLoginUserCacheTimer(loginUserCache, allPlaceLoginTokenCache);
//    }

}
